var express = require('express');
var router = express.Router();
var md5 = require('blueimp-md5')
const UserModel = require("../db/userModel");
var createtoken = require('../utils/createToken')
//用户登录
module.exports = (req, res) => {
    const {username, password} = req.body
    UserModel.findOne({username:username}, function (error, user) {
        //如果找到了用户
        if (user) {
            //如果密码正确
            if (user.password === md5(password)) {
                res.send({
                    code: 1,
                    user: {
                        username: user.username,
                        id: user._id,
                        token: createtoken.createToken(user.username, user._id)
                    }
                })
            } else {
                //密码不正确
                res.status(400).send({
                    status: 400,
                    message: "用户名或密码不正确"
                })
            }
        } else {
            //没有找到用户
            res.status(400).send({
                status: 400,
                message: "用户名或密码不正确"
            })
        }
    })
}